package com.zhiqianbao.utils;

import android.app.Activity;
import android.text.TextUtils;

import com.zhiqianbao.entity.Result;
import com.zhiqianbao.shared.SharedPreferanceUtils;

import java.util.HashMap;
import java.util.Map;

import cn.trinea.android.common.util.StringUtils;

/**
 * 连连支付工具类
 * Created by Administrator on 2017/4/26.
 */
public class LianLianUtils {
    //银行卡状态已绑定
    public static final String CARD_STATUS_BIND = "bind";
    //银行卡状态未绑定
    public static final String CARD_STATUS_UNBIND = "unbind";
    private static SharedPreferanceUtils sharedPreferanceUtils;

    public static void checkLianlianBindCard(final Activity context ,final Handler handler){
        sharedPreferanceUtils = new SharedPreferanceUtils(context);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, String> params = new HashMap<>();
                params.put("userId",Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                params.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                String result = HttpUtils.httpPostString(Constants.API_ROOT + "/loan/check/lianlian/state", params,context);
                if (!StringUtils.isBlank(result)) {
                    Result<String> data;
                    try {
                        data = Result.fromJson(result, String.class);
                    }catch (NullPointerException exception){
                        data=null;
                    }
                    if (data!=null){
                    if(Constants.DEFAULT_SUCCESS_CODE.equals(data.getCode())){
                        if (!TextUtils.isEmpty(data.getData())){
                            sharedPreferanceUtils.setResultO(data.getData());
                        }

                        if("1".equals(data.getData())){
                            handler.process(CARD_STATUS_BIND);
                        }else{
                            handler.process(CARD_STATUS_UNBIND);
                        }
                    }else{
                        handler.error(data.getMsg());
                    }
                }else {
                        handler.error(Constants.HTTP_ERROR_MESSAGE);
                    }
                }
            }
        }).start();
    }
    public interface Handler{
        void process(String result);
        void error(String reason);
    }
}
